<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/28/028
 * Time: 11:06
 */

namespace app\Lib\Model\Order;


use app\Lib\Model\DbModel\OrderInfo;
use app\Lib\Model\User\User;

class GetOrder
{
    private $user;
    private $limit = 10;
    private $page = 1;
    private $keyword;
    private $keywordType;
    private $order_status;
    private $isPage;
    private $count = 0;
    private $map;
    private $Db;
    /**
     * @param mixed $isPage
     */
    public function setIsPage($isPage)
    {
        $this->isPage = $isPage;
    }
    public function __construct(User $user)
    {
        $this->user = $user;
        $this->Db   = new OrderInfo();
    }

    /**
     * @param int $limit
     */
    public function setLimit($limit)
    {
        $this->limit = $limit;
    }

    /**
     * @param int $page
     */
    public function setPage($page)
    {
        $this->page = $page;
    }

    /**
     * @param mixed $keyword
     */
    public function setKeyword($keyword)
    {
        $this->keyword = $keyword;
    }

    /**
     * @param mixed $keywordType
     */
    public function setKeywordType($keywordType)
    {
        $this->keywordType = $keywordType;
    }

    /**
     * @param mixed $order_status
     */
    public function setOrderStatus($order_status)
    {
        $this->order_status = $order_status;
    }


    public function run(){
        $this->getMap();
        $this->getCount();
        if($this->isPage){
            return $this->count;
        }
        $data = $this->getList();
        return $data;
    }

    private function getMap(){
        $map = [];
        if($this->keyword && $this->keywordType){
            switch ($this->keywordType){
                case 'introducer_phone':
                    $map[$this->Db->_introducer_phone] = $this->keyword;
                    break;
                case 'order_number':
                    $map[$this->Db->_order_number] = $this->keyword;
                    break;
                case 'user_name':
                    $map[$this->Db->_user_name] = $this->keyword;
                    break;
                case 'user_address':
                    $map[$this->Db->_user_address] = ['like','%'.$this->keyword.'%'];
                    break;
                case 'design_user':
                    $uid = User::getAccountToUid($this->keyword);
                    if($uid>0){
                        $map[$this->Db->_design_uid] = $uid;
                    }
                    break;
                case 'drawing_user':
                    $uid = User::getAccountToUid($this->keyword);
                    if($uid>0){
                        $map[$this->Db->_drawing_uid] = $uid;
                    }
                    break;
            }
        }

        if($this->user->getRoleClass()->isDesigner()){
            $map[$this->Db->_design_uid] = $this->user->getUid();
        }

        if($this->user->getRoleClass()->isEngineeringDepartmentSupervision()){
            $map[$this->Db->_design_uid] = $this->user->getUid();
        }
        $this->map = $map;
    }

    private function  getCount(){
        $table =  $this->Db;
        $count = $table->where($this->map)->count('id');
        $this->count = $count;
    }

    private function getList(){
        if($this->count == 0){
            return [];
        }
        $table =  $this->Db;
        $orderId = $table->where($this->map)
            ->limit(($this->page-1)*$this->limit,$this->limit)
            ->order('id desc')->column($table->_order_number);
        $data = [];
        foreach ($orderId as $v){
            $model = (new Order($v));
            $superUid = $model->get_site_supervisor_uid();
            $supervisor_name = '';
            if($superUid>0){
                $supervisor_name=$model->get_site_supervisor_user()->getNickName();
            }
            $data[$v]=[
                $table->_id =>$model->get_order_id(),
                $table->_order_number =>$v,
                $table->_user_name =>$model->get_user_name(),
                $table->_user_phone =>$model->get_user_phone(),
                'style_str'=>$model->get_style_obj()->getTagName(),
                $table->_order_style=>$model->get_style_obj()->getTagId(),
                'house_type_str'=>$model->get_house_type_obj()->getTagName(),
                $table->_house_type =>$model->get_house_type_obj()->getTagId(),
                $table->_user_address =>$model->get_user_address(),
                $table->_create_time =>$model->get_create_time(),
                'create_time_str' =>$model->get_create_time_str(),
                $table->_orderStatus =>$model->get_orderStatus(),
                'orderStatus_str'=>$model->get_orderStatus_str(),
                'starting_time'=>$model->get_starting_time(),
                'starting_time_str'=>$model->get_starting_time_str(),
                'duration'=>$model->get_duration(),
                'design_user'=>$model->get_design_user()->getUid(),
                'design_user_name'=>$model->get_design_user()->getNickName(),
                'supervisor_uid'=>$superUid,
                'supervisor_name'=>$supervisor_name,
            ];
        }
        return $data;
    }
}